#Imports
import os, sys
import pygame

class_folder = os.path.join(os.getcwd(),'classes')
if class_folder not in sys.path:
    sys.path.insert(0,class_folder)
from maze import *
from goal import *
from player import *
import functions

#define constants
EASY = 0
MEDIUM = 1
HARD = 2

#Config
def Config(Object):
    pass

Config.screen_size = (1280,720)
Config.tile_size = 40
Config.wall_thickness = 8
Config.difficulty = EASY

Config.difficulty_multiplier = (10,10,20)
Config.colors = {'black': (0,0,0),
                'white': (255,255,255),
                'red': (255,0,0),
                'fire_color': (255,168,0),
                'wall_color': (73,33,33),
                'text_color': (200,200,200)}
Config.firewall_mode = Config.difficulty > EASY
Config.cell_image = functions.load_image('grass.png')

def main():

    pygame.init()
    screen = pygame.display.set_mode(Config.screen_size)
    pygame.display.set_caption('Maze')

    maze = Maze(Config)
    goal = Goal(maze, Config.tile_size)
    player = Player( (maze.start_cell.x,maze.start_cell.y), Config )


    #Layers are rendered in order of their index
    render_layers = [pygame.sprite.RenderPlain() for i in range(3)]

    #these references to the layers make it easy to understand what each layer is used for
    active_layer = render_layers[2]
    walls_layer = render_layers[1]
    floor_layer = render_layers[0]

    active_layer.add(player)
    active_layer.add(goal)
    walls_layer.add(maze.make_walls())
    floor_layer.add(maze.show_cells())

    clock = pygame.time.Clock()

    #Main Game Loop
    done = False
    while not done:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    player.change_speed(-3,0)
                if event.key == pygame.K_RIGHT:
                    player.change_speed(3,0)
                if event.key == pygame.K_UP:
                    player.change_speed(0,-3)
                if event.key == pygame.K_DOWN:
                    player.change_speed(0,3)

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    player.change_speed(3,0)
                if event.key == pygame.K_RIGHT:
                    player.change_speed(-3,0)
                if event.key == pygame.K_UP:
                    player.change_speed(0,3)
                if event.key == pygame.K_DOWN:
                    player.change_speed(0,-3)

        if player.update(walls_layer, Config.firewall_mode):
            done = True

        # If the player hits the goal the game ends
        if goal.player_hit(player):
            done = True

        for layer in render_layers:
            layer.draw(screen)

        pygame.display.flip()

        clock.tick(40)

    pygame.quit()

# the reason we made a main() function and then we call it here at the bottom of
# the file is that we want the code to not break if it's included in a larger
# project. If this file is run on its own then __name__ will be "__main__", and
# that means we want to do all the stuff in the main() function. If it's
# included from another python program, then __name__ won't be main, and we
# don't want to run the code right away. This is just a safe way of doing it,
# and you'll see this pattern a lot in python
if __name__ == '__main__': main ( )
